package handler

import (
	"exam/api/request"
	"exam/internal/dao"
	"exam/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Register(c *gin.Context) {
	var req request.Register
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneByFields(&model.User{}, &user)
	if user.Account == req.Account {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "此用户已存在",
			"data": nil,
		})
		return
	}
	user = model.User{
		Account:  req.Account,
		PassWord: req.PassWord,
	}
	if !dao.Create(&user) {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": user,
	})
}

func Login(c *gin.Context) {

}
